package com.nursinghome.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nursinghome.admin.mapper.RoomMapper;
import com.nursinghome.admin.service.RoomService;
import com.nursinghome.bean.GoodsRoom;
import com.nursinghome.bean.PageFilter;
import com.nursinghome.bean.Room;
import com.nursinghome.common.mapper.GoodsRoomMapper;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private GoodsRoomMapper goodsRoomMapper;
    //@Cacheable(value = "nursing_home_sys", key = "'roomList'", unless="#result == null || #result.size() == 0")
    @Override
    public PageFilter<Room> selectAll(String pageNum, String pageSize, Room room) {
        room.setPageNum(pageNum);
        room.setPageSize(pageSize);
        List<Room> list = roomMapper.selectAll(room);
        return room.getPageFilter(room, list);
    }

    @Override
    public Room selectByRid(Integer rid) {
        return roomMapper.selectByRid(rid);
    }

    @Override
    public Room selectByRno(String rno) {
        Room room = roomMapper.selectByRno(rno);
        return room;
    }

    //@CacheEvict(value = "nursing_home_sys", key = "'roomList'")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(Room room) throws NurisingHomeException {
        //判断房间好是否存在
        Room byRno = roomMapper.selectByRno(room.getRno());
        if (byRno != null){
            throw new NurisingHomeException("上传的房间中有重复，请重新上传！");
        }
        // 新增房间设施
        Integer insert = roomMapper.insert(room);
        if (insert != 1){
            throw new NurisingHomeException("新增房间失败");
        }else {
            if (!ObjectUtil.isEmpty(room.getGoods())){
                for (Integer goodsId : room.getGoodsIds()) {
                    GoodsRoom goodsRoom = new GoodsRoom(null, goodsId, room.getRid());
                    goodsRoomMapper.insert(goodsRoom);
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Room room) throws NurisingHomeException {
        Room byRno = roomMapper.selectByRno(room.getRno());
        Room byRid = roomMapper.selectByRid(room.getRid());
        //判断修改后的房间号是否已经存在
        /*
         *判断byRno不能为空且byRno的rno和byRid的rno不一样
         * 则是和其他房间号重复
         * 如果是修改为空闲检查是否还有人入住
         * */
        if (byRno!=null && !byRno.getRno().equals(byRid.getRno())){
            throw new NurisingHomeException("房间号已经存在");
        }
        if (room.getRstate() == 1){
            if (!ObjectUtil.isEmpty(byRid.getElderlies())){
                throw new NurisingHomeException("该房间还有人入住，不能修改为空闲");
            }
        }
        Integer update = roomMapper.update(room);
        if (update != 1){
            throw new NurisingHomeException("修改失败");
        }else if (room.getRno() != null){ //判断是不是批量处理状态
            // 删除所有房间的设施
            goodsRoomMapper.delete(room.getRid());
            for (Integer goodsId : room.getGoodsIds()) {
                GoodsRoom goodsRoom = new GoodsRoom(null, goodsId, room.getRid());
                goodsRoomMapper.insert(goodsRoom);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Integer rid) throws NurisingHomeException {
        //判断房间不存在
        Room byRid = roomMapper.selectByRid(rid);
        if (byRid == null || !byRid.getElderlies().isEmpty()){
            throw new NurisingHomeException("房间不存在，不能删除");
        }
        goodsRoomMapper.delete(rid);
        return roomMapper.delete(rid) == 1;
    }

}
